/**
 *   Created by lihao on 2017/10/31
 */

module.exports = function(grunt){

    'use strict';

//    Project configuration
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify : {
            options: {
                banner : '/* <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            build : {
                src : ['public/js/ctrl/*.js'],
                dest : 'public/js/build/<%= pkg.name %>.min.js'
            },
            cssmin: {
                css: {
                    src : ['public/css/index.css'],
                    dest: 'public/css/<%=  pkg.name %>.min.css'
                }
            }
        }
    });

    //加载包含  "uglify"   任务插件
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-string-replace');
    grunt.loadNpmTasks('grunt-contrib-cssmin');

    // 默认被执行的任务列表
    grunt.registerTask('admin', ['uglify','cssmin']);

    /**
     * 自定义任务
     *  1. 开发指令    grunt pom:dev
     *  2. 测试指令    grunt pom:uat
     *  3. 正式指令    grunt pom:pro
     */
    grunt.task.registerTask('pom','environment',function(task){
        var localObj = {};
        var contents = '/**Created on ' + grunt.template.today('yyyy-mm-dd') + '*/\n\r'
                        + '\'use strict\';\n\r' + 'module.exports = ';

        var config = grunt.file.readJSON('./config/pom.json');
        localObj = config[task];

        contents += JSON.stringify(localObj) + ';';

        grunt.file.write('./routes/resource/base-config.js', contents, {
            encoding : 'utf8'
        });

    });

};
